//
//  ComposeViewController.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/17.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ComposeViewController: UIViewController{
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
       
        setupUI()
        
        
    }

    
    private func setupUI(){
        setupNav()
        setupTextView()
        setupToolBar()
        setSelectView()
        registerNotification()
        
    }
    //设置导航控制器
    private func setupNav(){
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: .Plain, target: self, action: #selector(self.closeBtnDidClick))
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: .Plain, target: self, action: #selector(self.postBtnDidClick))
        
        self.navigationItem.rightBarButtonItem?.enabled = false
        //导航控制器的标题视图
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        self.navigationItem.titleView = titleView
        
        //设置titleview的属性
        let titleLabel = UILabel(title: "发微博", fontSize: 17, color: UIColor.grayColor())
        let nameLabel = UILabel(title: UserAccountViewModel().userName ?? "", fontSize: 14, color: UIColor.lightGrayColor())
        titleView.addSubview(titleLabel)
        titleView.addSubview(nameLabel)
        
        //添加约束
        titleLabel.snp_makeConstraints { (make) in
            make.top.equalTo(titleView.snp_top)
            make.centerX.equalTo(titleView.snp_centerX)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp_bottom)
            make.centerX.equalTo(titleLabel.snp_centerX)
        }
        
        
        
        
        
        
    }
    //设置输入框
    private func setupTextView(){
        self.view .addSubview(textview)
        
        textview.snp_makeConstraints { (make) in
            make.left.right.top.equalTo(self.view)
            make.height.equalTo(self.view.bounds.height / 2)
            
        }
        
        //放入展位文本
        textview.addSubview(placeHoderLabel)
        placeHoderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(textview.snp_top).offset(10)
            make.left.equalTo(textview.snp_left).offset(10)
        }
        
        
    }
    //设置底部工具栏
    private func setupToolBar(){
        self.view .addSubview(toolbar)
        
        toolbar.snp_makeConstraints { (make) in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(40)
            
        }
        
    //设置toolbar的点击事件
        var items = [UIBarButtonItem]()
        
        let itemSettings = [
            ["imageName": "compose_toolbar_picture","actionName":"selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background"],
            ["imageName": "compose_add_background"]
            ]
        for dict in itemSettings {
            let item = UIBarButtonItem(imageName: dict["imageName"]!, target: self, actionName: dict["actionName"])
            items .append(item)
            
            //为每个item加入弹簧
            let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(space)
        }
        //取消掉最后一个弹簧
        items .removeLast()
        toolbar.items = items
        
    }
    //设置图片添加视图
    private func setSelectView(){
        self.addChildViewController(selectorImgVC)
        self.view .addSubview(selectorImgVC.view)
        
        self.view.bringSubviewToFront(toolbar)
        
        selectorImgVC.view.snp_makeConstraints { (make) in
            make.right.left.bottom.equalTo(view)
            make.height.equalTo(0)
        }
        
    }
    //注册通知
    private func registerNotification(){
        //添加监听键盘改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.keyboardWillChange(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //MARK:-
    //MARK:监听键盘变化后改变toolbar位置
    @objc private func keyboardWillChange(n:NSNotification){
        //取得键盘冒出的时候的一些参数
        let duration = n.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let rect = (n.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        
        toolbar.snp_updateConstraints { (make) in
            make.bottom.equalTo(-screenH + rect.origin.y)
        }
        
        UIView.animateWithDuration(duration) {
            self.view.layoutIfNeeded()
        }
        
       
        
    }

    
    //MARK:-
    //MARK:toolbar点击事件
    @objc private func selectPicture(){
        print("你点击了图片加载按钮")
        selectorImgVC.view.snp_updateConstraints { (make) in
            make.height.equalTo(280)
        }
        
    }

    
    //MARK:-
    //MARK:导航栏按钮点击事件
    @objc private func closeBtnDidClick(){
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    @objc private func postBtnDidClick(){
        
    }

    
    //MARK:-
    //MARK:懒加载属性
    private lazy var selectorImgVC :PictureSelectorCollectionViewController = PictureSelectorCollectionViewController()
    private lazy var toolbar :UIToolbar = UIToolbar()
    private lazy var textview :UITextView = {
        let tv = UITextView()
        
        tv.font = UIFont.systemFontOfSize(18)
        tv.textColor = UIColor.darkGrayColor()
        tv.delegate = self
        
        //设置键盘消失模式
        tv.keyboardDismissMode = .OnDrag
        //设置弹簧效果
        tv.alwaysBounceVertical = true
        return tv
    }()
    private lazy var placeHoderLabel :UILabel = UILabel(title: "分享新鲜事。。。。。", fontSize: 18, color: UIColor.lightGrayColor())

}


//MARK:-
//MARK:textview的代理方法
extension ComposeViewController:UITextViewDelegate{
    func textViewDidChange(textView: UITextView) {
        self.placeHoderLabel.hidden = textView.hasText()
        self.navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
}

